/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var express = require('express');
var moment = require("moment");
var router = express.Router();
var co = require('co');
var seqModule = require('./createModules');
var seq = new seqModule();
var fs = require('fs');
var nodeExcel = require('excel-export');

var formidable = require('formidable');
var getIP = require('./getIP');

//查询用户信息/分页
router.get('/getMemberRespresentPage', function (req, res, next) {
    co(function () {
        var page = parseInt(req.query.page);
        var params = {};
        var paramT = {};
        var paramV = {};
        var include = [{ model: seq.memberModule }, { model: seq.departmentModule }];
        paramT["include"] = include;
        if (req.query.userId != undefined) {
            params["userId"] = req.query.userId;
        }
        //模糊查询
        if (req.query.name != undefined) {
            params["name"] = { "$like": "%" + req.query.name + "%" };

        }
        if (req.query.tel != undefined) {
            params["tel"] = { "$like": "%" + req.query.tel + "%" };

        }
        if (req.query.email != undefined) {
            params["email"] = { "$like": "%" + req.query.email + "%" };

        }
        if (req.query.pTitle != undefined) {
            params["$or"] = { "name": { "$like": "%" + req.query.pTitle + "%" }, "tel": { "$like": "%" + req.query.pTitle + "%" } }
        }
        if (req.query.province != undefined) {
            params["province"] = { "$like": "%" + req.query.province + "%" };

        } if (req.query.city != undefined) {
            params["city"] = { "$like": "%" + req.query.city + "%" };
        }
        if (req.query.departFirst != undefined) {
            params["departFirst"] = { "$like": "%" + req.query.departFirst + "%" };
        }
        if (req.query.departSecond != undefined) {
            params["departSecond"] = { "$like": "%" + req.query.departSecond + "%" };
        }
        if (req.query.departThird != undefined) {
            params["departThird"] = { "$like": "%" + req.query.departThird + "%" };
        }
        if (req.query.post != undefined) {
            params["post"] = { "$like": "%" + req.query.post + "%" };
        }
        if (req.query.filed4 != undefined) {
            params["filed4"] = req.query.filed4;
        }
        paramT["where"] = params;
        paramV["where"] = params;
        paramT["order"] = [["create_time", "DESC"]];
        if (req.query.page != undefined && req.query.rows != undefined) {
            paramT["offset"] = parseInt(req.query.page);
            paramT["limit"] = parseInt(req.query.rows);
        }
        seq.memberRespresentModule.findAll(paramT).then(function (result) {
            seq.memberRespresentModule.count(paramV).then(function (count) {
                res.json({ "success": true, "page": page, "total": count, "result": result });
            })
        });
    }).catch(function (e) {
        res.json({ 'success': false, 'msg': '查询失败' });
        console.log(e);
    })
});

router.get('/getRespresentPages', function (req, res, next) {
    co(function () {
        var page = parseInt(req.query.page);
        var params = {};
        var paramT = {};
        if (req.query.userId != undefined) {
            params["userId"] = req.query.userId;
        }
        //模糊查询
        if (req.query.name != undefined) {
            params["name"] = { "$like": "%" + req.query.name + "%" };

        }
        if (req.query.tel != undefined) {
            params["tel"] = { "$like": "%" + req.query.tel + "%" };

        }
        if (req.query.email != undefined) {
            params["email"] = { "$like": "%" + req.query.email + "%" };

        }
        if (req.query.pTitle != undefined) {
            params["$or"] = { "name": { "$like": "%" + req.query.pTitle + "%" }, "tel": { "$like": "%" + req.query.pTitle + "%" } }
        }
        if (req.query.province != undefined) {
            params["province"] = { "$like": "%" + req.query.province + "%" };

        } if (req.query.city != undefined) {
            params["city"] = { "$like": "%" + req.query.city + "%" };

        }
        if (req.query.departFirst != undefined) {
            params["departFirst"] = { "$like": "%" + req.query.departFirst + "%" };

        }
        if (req.query.departSecond != undefined) {
            params["departSecond"] = { "$like": "%" + req.query.departSecond + "%" };
        }
        if (req.query.departThird != undefined) {
            params["departThird"] = { "$like": "%" + req.query.departThird + "%" };
        }
        if (req.query.post != undefined) {
            params["post"] = { "$like": "%" + req.query.post + "%" };
        }
        paramT["where"] = params;
        paramT["order"] = [["create_time", "DESC"]];
        if (req.query.page != undefined && req.query.rows != undefined) {
            paramT["offset"] = parseInt(req.query.page);
            paramT["limit"] = parseInt(req.query.rows);
        }
        seq.memberRespresentModule.findAndCountAll(paramT).then(function (result) {
            res.json({ "success": true, "page": page, "total": result.count, "result": result.rows });
        });
    }).catch(function (e) {
        res.json({ 'success': false, 'msg': '查询失败' });
        console.log(e);
    })
});



//修改用户信息
router.get('/updMemberRespresent', function (req, res, next) {
    co(function () {
        var params = {};
        var paramT = {};
        var paramU = {};
        //根据id
        if (req.query.userId != undefined) {
            paramU["userId"] = req.query.userId;
        }

        if (req.query.empNum != undefined) {
            params["empNum"] = req.query.empNum;

        }
        if (req.query.name != undefined) {
            params["name"] = req.query.name;

        }
        if (req.query.tel != undefined) {
            params["tel"] = req.query.tel;

        }
        if (req.query.email != undefined) {
            params["email"] = req.query.email;

        }
        if (req.query.sex != undefined) {
            params["sex"] = parseInt(req.query.sex);

        }

        if (req.query.province != undefined) {
            params["province"] = req.query.province;

        }

        if (req.query.filed1 != undefined) {
            params["filed1"] = req.query.filed1;

        }
        if (req.query.filed2 != undefined) {
            params["filed2"] = req.query.filed2;

        }
        if (req.query.filed3 != undefined) {
            params["filed3"] = req.query.filed3;

        }
        if (req.query.filed4 != undefined) {
            params["filed4"] = req.query.filed4;
        }
        if (req.query.filed5 != undefined) {
            params["filed5"] = req.query.filed5;

        } if (req.query.departFirst != undefined) {
            params["departFirst"] = { "$like": "%" + req.query.departFirst + "%" };

        }
        if (req.query.departSecond != undefined) {
            params["departSecond"] = { "$like": "%" + req.query.departSecond + "%" };
        }
        if (req.query.departThird != undefined) {
            params["departThird"] = { "$like": "%" + req.query.departThird + "%" };
        }
        if (req.query.post != undefined) {
            params["post"] = { "$like": "%" + req.query.post + "%" };
        }

        paramT["where"] = paramU;
        seq.memberRespresentModule.update(params, paramT).then(function (results) {
            res.json({ "success": true, "msg": "修改成功", "rows": results });
        });
    }).catch(function (e) {
        res.json({ 'success': false, 'msg': '查询失败' });
        console.log(e);
    })
});

router.get('/addMemberRespresent', function (req, res, next) {
    co(function () {
        seq.memberRespresentModule.create(req.query).then(function (results) {
            res.json({ "success": true, "rows": results });
        });
    }).catch(function (e) {
        res.json({ 'success': false, 'msg': '添加失败' });
        console.log(e);
    })
});

// //删除
router.get('/deleteMemberRespresent', function (req, res, next) {
    co(function () {
        var params = req.query.params;
        var arr = params.split(",");
        var paramT = [arr];
        seq.memberRespresentModule.destroy({ 'where': { 'userId': paramT } }).then(function (results) {
            res.json({ "success": true, "msg": "删除成功", "rows": results });
        });
    }).catch(function (e) {
        res.json({ 'success': false, 'msg': '删除失败' });
        console.log(e);
    })
});

//导出代表
router.get('/getmemberRespresentExcel', function (req, res, next) {
    co(function () {
        var params = {};
        var paramT = {};
        var include = [{ model: seq.memberModule }, { model: seq.departmentModule }];
        paramT["include"] = include;
        if (req.query.userId != undefined) {
            params["userId"] = req.query.userId;
        }
        //模糊查询
        if (req.query.name != undefined) {
            params["name"] = { "$like": "%" + req.query.name + "%" };

        }
        if (req.query.tel != undefined) {
            params["tel"] = { "$like": "%" + req.query.tel + "%" };

        }
        if (req.query.email != undefined) {
            params["email"] = { "$like": "%" + req.query.email + "%" };

        }
        if (req.query.pTitle != undefined) {
            params["$or"] = { "name": { "$like": "%" + req.query.pTitle + "%" }, "tel": { "$like": "%" + req.query.pTitle + "%" } }
        }
        if (req.query.province != undefined) {
            params["province"] = { "$like": "%" + req.query.province + "%" };

        } if (req.query.city != undefined) {
            params["city"] = { "$like": "%" + req.query.city + "%" };

        }
        if (req.query.departFirst != undefined) {
            params["departFirst"] = { "$like": "%" + req.query.departFirst + "%" };

        }
        if (req.query.departSecond != undefined) {
            params["departSecond"] = { "$like": "%" + req.query.departSecond + "%" };
        }
        if (req.query.departThird != undefined) {
            params["departThird"] = { "$like": "%" + req.query.departThird + "%" };
        }
        if (req.query.post != undefined) {
            params["post"] = { "$like": "%" + req.query.post + "%" };
        }
        if (req.query.filed4 != undefined) {
            params["filed4"] = req.query.filed4;
        }
        paramT["where"] = params;
        paramT["order"] = [["create_time", "DESC"]];
        seq.memberRespresentModule.findAll(paramT).then(function (result) {

            var trasations = [];
            var conf = {};
            conf.stylesXmlFile = "styles.xml";

            trasations = result;
            conf.name = "memberRespresent";
            var vac = new Array();
            //设置表头
            conf.cols = [
                {
                    caption: '姓名',
                    type: 'string',
                    width: 10.6
                },
                {
                    caption: '手机号',
                    type: 'string',
                    width: 28
                }, {
                    caption: '邮箱',
                    type: 'string',
                    width: 10
                },
                {
                    caption: '性别',
                    type: 'string',
                    width: 10
                },
                {
                    caption: '所在部门',
                    type: 'string',
                    width: 10
                },
                {
                    caption: '职位',
                    type: 'string',
                    width: 10
                },
                {
                    caption: '绑定医生数',
                    type: 'string',
                    width: 10
                },
                {
                    caption: '注册时间',
                    type: 'string',
                    width: 10
                }
            ];
            console.log(trasations.length + "export");
            for (var i = 0; i < trasations.length; i++) {
                var temp = new Array();
                temp[0] = trasations[i].name + "";
                temp[1] = trasations[i].tel + "";
                temp[2] = trasations[i].email + "";
                var sex = "";
                if (trasations[i].sex == 1) {
                    sex = "男";
                } else if (trasations[i].sex == 2) {
                    sex = "女";
                }
                temp[3] = sex + "";
                var depart = "";
                if (trasations[i].Department != null) {
                    depart = trasations[i].Department.name;
                }
                temp[4] = depart + "";
                temp[5] = trasations[i].post + "";
                temp[6] = trasations[i].members.length + "";
                temp[7] = trasations[i].create_time + "";



                //temp[10] = (trasations[i].state) == "0"?"未付款或未持久化":"已付款或已持久化";
                vac.push(temp);
            };


            conf.rows = vac;//conf.rows只接受数组
            var result = nodeExcel.execute(conf);
            res.setHeader('Content-Type', 'application/vnd.openxmlformats');
            res.setHeader("Content-Disposition", "attachment; filename=" + "memberRespresent.xlsx");
            res.end(result, 'binary');
        });
    }).catch(function (e) {
        res.json({ 'success': false, 'msg': '查询失败' });
        console.log(e);
    })
});


module.exports = router;